package com.jcxy.controller;

import com.jcxy.mapper.DeptMapper;
import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.mapper.PositionMapper;
import com.jcxy.pojo.Dept;
import com.jcxy.pojo.Position;
import com.jcxy.service.DeptService;
import com.jcxy.util.PageBean;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/deptManage")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private DictionaryMapper dictionaryMapper;

    /**
     * 跳转到deptEdit.jsp页面
     * @param request
     * @return
     */
    @RequestMapping("toDeptEditPage.do")
    public String toDeptEditPage(HttpServletRequest request){

        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());
        List<Dept> deptList = deptMapper.queryDept(dto);

        request.setAttribute("edit_dept",deptList.get(0));
        request.setAttribute("pageType",1);

        return "userManage/deptEdit";
    }

    /**
     * 通过分页获得部门列表
     * @return
     */
    @RequestMapping("/getDeptListByPage.do")
    @ResponseBody
    public ResponseData getDeptListByPage(HttpServletRequest request){


        //将参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());

        //调用业务逻辑层方法，查询角色列表
        PageBean pageBean = deptService.queryListByPage(
                dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 获取职位列表信息，以及部门拥有的职位信息
     * @return
     */
    @RequestMapping("/getJobList.do")
    @ResponseBody
    public ResponseData getJobList(Integer dept_id){

        //用于存放发送到前端的数据
        List data = new ArrayList();

        //获取所有职位信息
        List<Position> allJobList = positionMapper.queryPosition(null);
        data.add(allJobList);

        //判断部门id是否为空
        if(dept_id != null){

            //获取职位所属的部门id
            List<Integer> jobIdList = positionMapper.queryPositionIdByDeptId(dept_id);
            data.add(jobIdList);
        }

        ResponseData ok = ResponseData.ok();
        ok.setData(data);
        return ok;
    }

    /**
     * 添加部门基本信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDept.do")
    public String addDept(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        deptService.insertDept(dto);

        //更新部门缓存
        request.getSession().setAttribute("dept",dictionaryMapper.queryDept());
        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/deptManage";
    }

    /**
     * 修改部门基本信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateDeptById.do")
    public String updateDeptById(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        deptService.updateDept(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/deptManage";
    }

    /**
     * 查询部门员工
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getDeptStaffList.do")
    @ResponseBody
    public ResponseData getDeptStaffList(HttpServletRequest request){

        Map dto = ParamsDto.getDto(request.getParameterMap());

        PageBean pageBean = deptService.queryDeptStaffListByPage(dto,//查询条件
                Integer.valueOf(dto.get("page").toString()),//当前页
                Integer.valueOf(dto.get("limit").toString()));//页容量

        return pageBean.getResponseData();
    }

    /**
     * 更新部门领导
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateDeptManager.do")
    @ResponseBody
    public ResponseData updateDeptManager(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        deptService.updateDeptManager(dto);

        ResponseData ok = ResponseData.ok();
        return ok;
    }

    /**
     * 获取部门领导姓名
     * @param dept_id
     * @return
     */
    @RequestMapping(value = "/getDeptManagerName.do",produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getDeptManagerName(Integer dept_id,HttpServletResponse response){

        response.setCharacterEncoding("utf8");
        return deptMapper.queryDeptManagerName(dept_id);
    }

    /**
     * 对员工进行调度
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/transferDeptStaff.do")
    @ResponseBody
    public ResponseData transferDeptStaff(HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        boolean isManager = deptService.updateUserDeptJob(dto);
        ResponseData ok = ResponseData.ok();
        if (isManager){
            ok.setCode(201);
        }
        return ok;
    }

    /**
     * 删除部门
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteDept.do")
    public String deleteDept(HttpServletRequest request) throws Exception {

        Map dto = ParamsDto.getDto(request.getParameterMap());

        deptService.deleteDept(dto);

        //更新部门缓存
        request.getSession().setAttribute("dept",dictionaryMapper.queryDept());
        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userManage/deptManage";
    }
}
